#include "./My_OLED_xx.h"

SNAKE_INFO snake_info[200]={0};//保存蛇身结构体
int snake_len=0;//蛇身长度

/*绘制蛇基本矩形*/
 void OLED_Snake_DrawFill(u8 x,u8 y,u8 c)
{
  LcdFill(x,y,x+4,y+4,c);
}
/*绘制食物*/
void OLED_Snake_DrawFood(u8 x,u8 y,u8 c)
{
  LcdFill(x,y,x+4,y+4,c);
}
/*绘制蛇身*/
void OLED_Snake_DrawBody(SNAKE_INFO *snake_p,int snake_len)
{
  u8 i=0;
  for(i=0;i<snake_len;i++)
  {
    OLED_Snake_DrawFill(snake_p[i].x,snake_p[i].y,1); 
  }
}
/*移动蛇身*/
void OLED_Snake_Move(u8 x,u8 y)
{
  u8 i=0;
  //将最后一个位置清除
  OLED_Snake_DrawFill(snake_info[0].x,snake_info[0].y,0);
  //移动坐标位置
  for(i=0;i<snake_len-1;i++)
  {
    snake_info[i]=snake_info[i+1];
  }
  snake_info[i].x=x;
  snake_info[i].y=y;
  //绘制蛇身
  OLED_Snake_DrawBody(snake_info,snake_len);
}
/*判断食物是否落在蛇身上*/
u8 OLED_Snake_JudgeFood(u8 x,u8 y)
{
  u8 i;
  for(i=0;i<snake_len;i++)
  {
    if(snake_info[i].x==x && snake_info[i].y==y)
    {
      return 1;//落在身身上
    }
  }
  return 0;//正常
}
/*绘制表盘*/
void OLED_DrawclockDial(void)
{
  OLED_ClearGram();//清空显示
  //圆
  OLED_Circle(64,32,30);
  OLED_Circle(64,32,29);
  OLED_Circle(64,32,1);
  //刻度
  u16 i=0;
  for(i=0;i<12;i++)
  {
    OLED_DrawAngleLine(64,32,i*30,29,5,1);//小时
  }
  for(i=0;i<60;i++)
  {
    OLED_DrawAngleLine(64,32,i*6,29,3,1);//分钟
  }
  OLED_Refresh();//更新显示
}
/*绘制指针*/
void OLED_DrawTime(u8 hour,u8 min,u8 sec)
{
  OLED_DrawAngleLine(64,32,(sec-1)*6+270,20,20,0);//清除上一秒
  //分针
  OLED_DrawAngleLine(64,32,(min-1)*6+270,15,15,0);//清除分针
  OLED_DrawAngleLine(64,32,min*6+270,15,15,1);//绘制当前位置
  //时针
  OLED_DrawAngleLine(64,32,(hour-1)*30+270+(min-1)*0.5,10,10,0);//清除时针
  OLED_DrawAngleLine(64,32,hour*30+min*0.5+270,10,10,1);
  
  OLED_DrawAngleLine(64,32,sec*6+270,20,20,1);//绘制当前位置
  OLED_Circle(64,32,1);//画圆心点
  OLED_Refresh();//更新显示
}
/*绘制日历*/
void OLED_DrawCalenda(u16 year,u8 mon,u8 day)
{
  u8 i=0;
  for(i=0;i<7;i++)
  {
    OLED_DispalyFont3(2+18*i,0,16,font_16[i]);//逐列式，高位在前
  }
  u8 week=RTC_GetWeek2(year,mon,1);
  u8 y=17;
  u8 x=0;
  
  x=4+(week-1)*18;
  char buff[20]={0};
  //显示日期
  if(Year_Judge(year))//判断闰平年
  {
    for(i=1;i<=mon_r[mon-1];i++)
    {
      if(i<10)
      {
        buff[0]=i+'0';
        buff[1]='\0';
      }
      else
      {
        buff[0]=(i/10)+'0';
        buff[1]=(i%10)+'0';
        buff[2]='\0';
      }
      OLED_DisplayStr(x,y,8,(u8*)buff);
      if(day==i)//当前日期,进行标记
      {
        OLED_DrawRectangle(x-2, y-1, x+12, y+8);//画矩形
      }
      x+=18;
      week++;
      if(week>7)
      {
        week=1;
        y+=9;
        x=4;
      }
    }
  }
  else 
  {
    for(i=1;i<=mon_p[mon-1];i++)
    {
      if(i<10)
      {
        buff[0]=i+'0';
        buff[1]='\0';
      }
      else
      {
        buff[0]=(i/10)+'0';
        buff[1]=(i%10)+'0';
        buff[2]='\0';
      }
      OLED_DisplayStr(x,y,8,(u8*)buff);
      x+=18;
      week++;
      if(week>7)
      {
        week=1;
        y+=8;
        x=2;
      }
    }      
  }
  OLED_Refresh();//更新显示
}


